from django.http import HttpResponse
from freedom99.articles.models import Article, Comment
from django.template import Context, loader
from django.http import Http404
from django.shortcuts import get_object_or_404


def list(request):
    list = Article.objects.all()

    templ = loader.get_template('articleList.html')
    conx = Context({
        'list': list,
    })
    return HttpResponse(templ.render(conx))



def detail(request, articleID):
    article = get_object_or_404(Article, id=articleID)
    comments = Comment.objects.filter(article=article)



    templ = loader.get_template('article.html')
    conx = Context({
    "article":article,
    "comments":comments})

    return HttpResponse(templ.render(conx))


def AddComment(request, articleID):
    article = get_object_or_404(Article, id=articleID)
    postData = request.POST

    comment = Comment(article=article, contact = postData["contact"],
    name = postData["name"], text= postData["text"])
    comment.save()



    return detail(request, articleID)
    